﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {
    
    public AudioClip FireClip;
    public AudioClip ExplodeClip;

    public string[] foods = new string[] {"Sphere", "Triangle", "Rectangle", "Pentagon", "Haxagon" };

    public Consts.BulletType bulletType;
    public float speed;
    public Vector3 Speed;

	// Use this for initialization
	void Start () {
        
	}

    // Update is called once per frame
    void Update () {
		
	}

    public void Move()
    {
        this.transform.position = new Vector3(
            this.transform.position.x + this.Speed.x * Datas.Singleton.Instance.playerRole.playerData.bulletSpeed, 
            this.transform.position.y + this.Speed.y * Datas.Singleton.Instance.playerRole.playerData.bulletSpeed, 
            this.transform.position.z
            );
        if (this.transform.position.x < -Consts.MapWidth
            || this.transform.position.x > Consts.MapWidth
            || this.transform.position.y < -Consts.MapHeight
            || this.transform.position.y > Consts.MapHeight)
        {
            Destroy(this.gameObject);
        }
    }

    public void Init(Vector3 s)
    {
        Debuger.Log("Recv Vector3: " + s.x + " " + s.y + " " + s.z);
        this.Speed = new Vector3(
            s.x * this.speed,
            s.y * this.speed,
            0);
        AudioSource.PlayClipAtPoint(FireClip, this.transform.localPosition, 10.0f);
    }

    public void Explode()
    {
        AudioSource.PlayClipAtPoint(ExplodeClip, this.transform.localPosition, 10.0f);
        Destroy(this.gameObject);
    }

    private void OnTriggerEnter(Collider other)
    {
        Debuger.Log(Time.time + " Trigger " + other.gameObject.name);

        switch (other.gameObject.name)
        {
            case "Sphere":
            case "Triangle":
            case "Rectangle":
            case "Pentagon":
            case "Haxagon":
                this.Explode();
                other.gameObject.GetComponent<Food>().blood += 1;
                if (other.gameObject.GetComponent<Food>().blood >= other.gameObject.GetComponent<Food>().Blood)
                {
                    // Destroy(other.gameObject);
                    other.gameObject.GetComponent<Food>().Init();
                }
                break;
            case "TankA(Clone)":
                Debuger.Log("Trigger Enemy");
                break;
        }

    }
}
